package com.darrenchan.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-11-27 13:44
 */
public class Q20 {
    public boolean isValid(String s) {
        if (s.length() == 0) {
            return true;
        }
        Map<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('[', ']');
        map.put('{', '}');
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}') {
                if (stack.isEmpty() || stack.contains(')') || stack.contains(']') || stack.contains('}')) {
                    return false;
                }
                if (s.charAt(i) == map.get(stack.peek())) {
                    stack.pop();
                } else {
                    stack.push(s.charAt(i));
                }
            } else {
                stack.push(s.charAt(i));
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new Q20().isValid("(])"));
    }
}
